﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace YmtAuth.Domain.Model.YmatouUser
{
    /// <summary>
    /// token 规则
    /// </summary>
    public static class UserTokenSpecifications
    {
        /// <summary>
        /// 匹配加密后的窜
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static IMongoQuery MathEncryptionToken(string token)
        {
            return Query.And(Query<UserToken>.EQ(e => e.TokenId, token)
                                    );
        }
        /// <summary>
        /// 匹配token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static IMongoQuery MatchToken(string token)
        {
            return Query.And(Query<UserToken>.EQ(e => e.RawToken, token)
                           );
        }

        /// <summary>
        /// 匹配token
        /// </summary>
        /// <param name="rawToken"></param>
        /// <param name="tokenSource"></param>
        /// <returns></returns>
        public static IMongoQuery MatchToken(string rawToken, string tokenSource)
        {
            if (string.Equals(tokenSource, "mainsite", StringComparison.InvariantCultureIgnoreCase))
            {
                tokenSource = YmtAuth.Domain.Model.Token.TokenSource.PC;
            }

            if (string.IsNullOrEmpty(tokenSource))
            {
                return Query<UserToken>.EQ(e => e.RawToken, rawToken);
            }
            else
            {
                tokenSource = tokenSource.ToLower();
                return Query.And(Query<UserToken>.EQ(e => e.RawToken, rawToken), Query<UserToken>.EQ(e => e.TokenSource, tokenSource));
            }
        }

        /// <summary>
        /// 匹配 userId, tokenSource
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static IMongoQuery MatchUserToken(int userId, string tokenSource)
        {
            if (string.Equals(tokenSource, "mainsite", StringComparison.InvariantCultureIgnoreCase))
            {
                tokenSource = YmtAuth.Domain.Model.Token.TokenSource.PC;
            }

            if (string.IsNullOrEmpty(tokenSource))
            {
                return Query<UserToken>.EQ(e => e.UserId, userId);
            }
            else
            {
                tokenSource = tokenSource.ToLower();
                return Query.And(Query<UserToken>.EQ(e => e.UserId, userId), Query<UserToken>.EQ(e => e.TokenSource, tokenSource));
            }
        }

        /// <summary>
        /// 匹配未过期的token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static IMongoQuery MatchNotExpiredToken(string token)
        {
            return Query.And(Query<UserToken>.EQ(e => e.RawToken, token)
                           , Query<UserToken>.GT(e => e.ExpiredTime, DateTime.Now));
        }
        /// <summary>
        /// 匹配过期的token
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public static IMongoQuery MatchExpiredToken(string token)
        {
            return Query.And(Query<UserToken>.EQ(e => e.RawToken, token)
                           , Query<UserToken>.LT(e => e.ExpiredTime, DateTime.Now));
        }
        /// <summary>
        /// 匹配用户过期的token
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static IMongoQuery MatchNotExpiredUserToken(int userId)
        {
            return Query.And(Query<UserToken>.EQ(e => e.UserId, userId)
                          , Query<UserToken>.GT(e => e.ExpiredTime, DateTime.Now));
        }
        /// <summary>
        /// 修改token过期时间
        /// </summary>
        /// <param name="exTime"></param>
        /// <returns></returns>
        public static UpdateBuilder<UserToken> ExtendTokenExpiredTimeUpdate(DateTime exTime)
        {
            return Update<UserToken>.Set(e => e.ExpiredTime, exTime);
        }
        /// <summary>
        /// 匹配tokenId
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static IMongoQuery MatchUserTokenMongoId(string id)
        {
            return Query<UserToken>.EQ(e => e.Id, id);
        }
        /// <summary>
        /// 匹配 userId
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static IMongoQuery MatchUserToken(int userId)
        {
            return Query<UserToken>.EQ(e => e.UserId, userId);
        }
        /// <summary>
        /// 获取token对应的集合
        /// </summary>
        /// <param name="sourceType">sourceType</param>
        /// <returns></returns>
        public static string TokenCollectionName(string sourceType)
        {
            if (string.IsNullOrEmpty(sourceType)) return "UserToken";
            if (sourceType.ToLower() == "matouapp") return "UserToken_MatouApp";
            if (sourceType.ToLower() == "app") return "UserToken_App";
            //if (sourceType.ToLower() == "weixinapp") return "UserToken_WeixinApp";
            return "UserToken";
        }
        /// <summary>
        /// 获取token对应的数据库
        /// </summary>
        /// <returns></returns>
        public static string TokenDatabaseName()
        {
            return "Ymt_Auth_Token";
        }
        public static string TokenMainTable() { return "UserToken"; }
    }
}
